package com.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.Blog;
import com.blog.pojo.Favorite;
import com.blog.pojo.ThumbsUp;
import com.blog.pojo.User;
import com.blog.service.FavoriteService;
import com.blog.service.ThumbsUpService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-06-09 17:07
 * FileName: FavoriteController
 * Description: 博客收藏控制层
 */
@RestController
@RequestMapping("Favorite")
@Slf4j
public class FavoriteController  extends BaseController{
    @Resource
    private FavoriteService favoriteService;

    /***
     * 查询用户点赞状态
     * @param blogId
     * @param session
     * @return
     */
    @GetMapping("/{blogId}/{uid}")
    public String selectThumbsUp(@Param("博客ID") @PathVariable("blogId")Long blogId, HttpSession session,
                                 @Param("用户id") @PathVariable("uid") Long uid){
        JSONObject object=new JSONObject();
        if(uid!=null){
            Favorite favorite=favoriteService.selectFavorite(blogId,uid);
            if(favorite!=null){
                object.put("flag",true);
                object.put("message","用户已收藏!");
            }else{
                object.put("flag",false);
                object.put("message","用户未收藏!");
            }
            return object.toJSONString();
        }
        object.put("flag",false);
        return object.toJSONString();
    }

    /***
     *
     * 用户点赞和取消点赞
     * @param blogId
     * @param uid
     * @return
     */
    @PostMapping("/{blogId}/{uid}")
    public String updateThumbsUp(@Param("博客ID") @PathVariable("blogId")Long blogId,@PathVariable("uid") Long uid){
        JSONObject object=new JSONObject();
        User user=checkUser(uid);
        if(user!=null){
            //查询当前登录用户是否已点赞该博客
            Favorite favorite=favoriteService.selectFavorite(blogId,user.getUid());
            if(favorite!=null){
                //删除点赞表对应数据
                favoriteService.deleteFavorite(blogId,user.getUid());
                object.put("flag",false);
                object.put("message","已取消收藏!");
                return object.toJSONString();
            }else{
                //修改博客点赞数量
                Favorite Favorite=new Favorite();
                Favorite.setBlogId(blogId).setUid(user.getUid());
                favoriteService.insetFavorite(Favorite);
                object.put("message","已收藏!");
                object.put("flag",true);
            }
        }
        return object.toJSONString();
    }

    @GetMapping("/{pageNumber}/{pageSize}/{uid}/{title}")

    public String getUserFavorite( @Param("开始页数") @PathVariable("pageNumber") Integer pageNumber,
                                   @Param("总页数") @PathVariable("pageSize") Integer pageSize,
                                   @Param("用户Id") @PathVariable("uid") Long uid,
                                   @Param("标题")  @PathVariable("title") String title){
        JSONObject object=new JSONObject();
        User user =checkUser(uid);
        log.info("验证用户是否登录");
        //验证用户是否登录
        if(user!=null){
            log.info("用户已登录,ID为:{}",user.getUid());
            log.info("获取用户收藏博客信息.....");
            PageInfo<Blog> PageInfo=favoriteService.queryFavoriteBlogsPage(pageNumber,pageSize,uid, title);
            object.put("Blogs",PageInfo.getList());
            object.put("flag",PageInfo.getList()!=null);
            object.put("total",PageInfo.getTotal());
            return object.toJSONString();
        }
        object.put("flag",false);
        return object.toJSONString();
    }

}
